<template>
    <div class="list-wrapper">
        <table class="list-table" border="0" cellspacing="0" cellpadding="0">
            <thead>
            <tr>
                <th>
                    <span class="icon add" @click="openAdd()"></span>
                </th>
                <th>序号</th>
                <th>资产编号</th>
                <th>资产名称</th>
                <th>规格型号</th>
                <th>原值</th>
                <th>数量</th>
                <th>增加方式</th>
                <th>累计折旧</th>
                <th>月折旧</th>
                <th>净值</th>
                <th>存放地点</th>
                <th>负责人</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,index) in list" :key="item.id">
                <td>
                    <span class="icon edit" @click="edit(item.id)"></span>
                    <span class="icon del" @click="Del(item.id)"></span>
                </td>
                <td>{{index + 1}}</td>
                <td>{{item.asset_name}}</td>
                <td>{{item.asset_num}}</td>
                <td>{{item.specification}}</td>
                <td>{{item.value}}</td>
                <td>{{item.quantity}}</td>
                <td>{{item.add_way}}</td>
                <td>{{item.accumulated_depreciation}}</td>
                <td>{{item.month_depreciation}}</td>
                <td>{{item.net_worth}}</td>
                <td>{{item.location}}</td>
                <td>{{item.leading_official}}</td>
            </tr>
            </tbody>
        </table>
        <div class="page">
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next, jumper"
                               :total="pageTotal">
                </el-pagination>
            </div>
        </div>
        <el-dialog title="添加其他资产信息" :visible.sync="pop" size="tiny">
            <el-form :model="addForm" :label-width="width" :rules="otherRules" ref="otherRulesName">
                <el-form-item label="资产编号" prop="asset_num">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_num" size="small" placeholder="请输入资产编号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="资产名称" prop="asset_name">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_name" size="small" placeholder="请输入资产名称"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="规格型号">
                    <el-col :span="span">
                        <el-input v-model="addForm.specification" size="small" placeholder="请输入规格型号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="原值">
                    <el-col :span="span">
                        <el-input v-model="addForm.value" size="small" placeholder="请输入原值"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="数量">
                    <el-col :span="span">
                        <el-input v-model="addForm.quantity" size="small" placeholder="请输入数量"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="增加方式">
                    <el-col :span="span">
                        <el-input v-model="addForm.add_way" size="small" placeholder="请输入增加方式"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="累计折旧">
                    <el-col :span="span">
                        <el-input v-model="addForm.accumulated_depreciation" size="small"
                                  placeholder="请输入累计折旧"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="月折旧">
                    <el-col :span="span">
                        <el-input v-model="addForm.month_depreciation" size="small" placeholder="请输入月折旧"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="净值">
                    <el-col :span="span">
                        <el-input v-model="addForm.net_worth" size="small" placeholder="请输入净值"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="存放地点">
                    <el-col :span="span">
                        <el-input v-model="addForm.location" size="small" placeholder="请输入存放地点"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="负责人">
                    <el-col :span="span">
                        <el-input v-model="addForm.leading_official" size="small" placeholder="请输入负责人"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="pop = false">取 消</el-button>
                <el-button type="primary" @click="change(true)">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="编辑其他资产信息" :visible.sync="Pop" size="tiny">
            <el-form :model="addForm" :label-width="width" :rules="otherRules" ref="otherRulesName">
                <el-form-item label="资产编号" prop="asset_num">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_num" size="small" placeholder="请输入资产编号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="资产名称" prop="asset_name">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_name" size="small" placeholder="请输入资产名称"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="规格型号">
                    <el-col :span="span">
                        <el-input v-model="addForm.specification" size="small" placeholder="请输入规格型号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="原值">
                    <el-col :span="span">
                        <el-input v-model="addForm.value" size="small" placeholder="请输入原值"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="数量">
                    <el-col :span="span">
                        <el-input v-model="addForm.quantity" size="small" placeholder="请输入数量"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="增加方式">
                    <el-col :span="span">
                        <el-input v-model="addForm.add_way" size="small" placeholder="请输入增加方式"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="累计折旧">
                    <el-col :span="span">
                        <el-input v-model="addForm.accumulated_depreciation" size="small"
                                  placeholder="请输入累计折旧"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="月折旧">
                    <el-col :span="span">
                        <el-input v-model="addForm.month_depreciation" size="small" placeholder="请输入月折旧"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="净值">
                    <el-col :span="span">
                        <el-input v-model="addForm.net_worth" size="small" placeholder="请输入净值"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="存放地点">
                    <el-col :span="span">
                        <el-input v-model="addForm.location" size="small" placeholder="请输入存放地点"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="负责人">
                    <el-col :span="span">
                        <el-input v-model="addForm.leading_official" size="small" placeholder="请输入负责人"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="Pop = false">取 消</el-button>
                <el-button type="primary" @click="change(false)">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    const other = {
        asset_num: '',
        asset_name: '',
        specification: '',
        value: '',
        quantity: '',
        add_way: '',
        accumulated_depreciation: '',
        month_depreciation: '',
        net_worth: '',
        location: '',
        leading_official: ''
    };
    export default {
        data() {
            return {
                Pop: false,
                pageTotal: 1,
                pageIndex: 1,
                pop: false,
                width: '200px',
                span: 15,
                addForm: _g.cloneJson(other),
                list: {},
                otherRules: {
                    asset_num: [{required: true, message: "请填写资产编号", trigger: "blur"}],
                    asset_name: [{required: true, message: "请填写资产名称", trigger: "blur"}]
                },
            }
        },

        created() {
            this.getList();
        },

        methods: {
            handleCurrentChange(val) {
                this.pageIndex = val;
                this.getList();
            },

            /* 获取数据 */
            getList() {
                let data = {page: this.pageIndex};
                this.apiPost("Operation/otherList", data).then(res => {
                    this.list = res.list;
                    this.pageTotal = parseInt(res.total);
                });
            },

            /* 打开新增窗口 */
            openAdd() {
                this.addForm = _g.cloneJson(other);
                this.pop = true;
                this.$refs.otherRulesName.resetFields();
            },
            /* 打开编辑页面 */
            edit(id) {
                let data = {id: id};
                this.apiPost('Operation/otherDetail', data).then(res => {
                    setTimeout(() => {
                        this.Pop = true;
                        this.$refs.otherRulesName.resetFields();
                        setTimeout(() => {
                            this.addForm = res;
                        }, 0);
                    }, 0);
                })
            },

            /* 提交数据 */
            change(judge) {
                this.$refs.otherRulesName.validate(valid => {
                    if (valid) {
                        judge ? this.pop = false : this.Pop = false;
                        let data = this.addForm;
                        this.apiPost(judge ? "Operation/otherAdd" : "Operation/otherEdit", data).then(res => {
                            let remind = res.error ? "error" : "success";
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getList();
                            } else {
                                judge ? this.pop = false : this.Pop = false;
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },

            /* 删除 */
            Del(id) {
                this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        let data = {id: id};
                        this.apiPost("Operation/otherDelete", data).then(res => {
                            if (!res.error) {
                                this.$message({
                                    type: "success",
                                    message: res.msg
                                });
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                });
                            }
                            this.getList();
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
        }
    }
</script>

<style scoped>
    .add {
        width: 25px;
        height: 25px;
        background: url(../../../img/add.png) no-repeat;
        background-size: 100%;
        cursor: pointer;
    }

    .edit {
        background: url(../../../img/edit.gif) no-repeat;
    }

    .del {
        background: url(../../../img/del.png) no-repeat;
    }

    .det {
        background: url(../../../img/detail.png) no-repeat;
        cursor: pointer;
    }
</style>